#! /bin/sh

#---------------------------------------------------------------------
#                              walkenfs                               
# 
# Tool to mount walkenfs via mount(8) or fstab(5).
#---------------------------------------------------------------------

#---------------------------------------------------------------------
#  Ok, you can't have whitespace willy-nilly in fstab.  You can have  
# '\040' but that sucks.  So we're going to use '::' to seperate
# stuff.  I won't support a literal '::' at this time through escaping
# or anything.  If it's a problem, drop me an email.
#
# The idea is to pack all the config onto the erlang line via application
# arguments except that:
#
#   * mount_point is gotten from the 2nd field of fstab(5) or the dir
#     argument to mount(8)
#   * mount_opts is gotten from the 4th field of fstab(5) or the -o
#     argument to mount(8)
# 
# example /etc/fstab entry:
# 
# walkenfs#-mnesia::dir::/var/flass::-s::mnesia   /walken fuse    defaults        0       0
# 
# example mount invocation:
# 
# mount -t fuse walkenfs#-mnesia::dir::/var/flass::-s::mnesia /walken
#---------------------------------------------------------------------


#ubsrvvmw# mount -t fuse flassfs#wtf:man -o warez /tmp
#arg: wtf:man
#arg: /tmp
#arg: -o
#arg: rw,warez
#
#flassfs#wtf\072man/     /mnt/warez      fuse    defaults        0       0
#ubsrvvmw# mount /mnt/warez
#arg: wtf:man/
#arg: /mnt/warez
#arg: -o
#arg: rw

is_alive () {
  epmd -names | grep "^name $1 at port " >/dev/null 2>/dev/null
}

optional () {
  local what="$1"
  local vars="$2"
  local default="$3"

  local val=`perl -e "(\\\$_) = @ARGV; m/$what::([^:]+)/ && print \"\\\$1\"" -- "$2"`

  printf "%s" "${val:-$default}"
}

required () {
  local what="$1"
  local vars="$2"

  perl -e "(\$_) = @ARGV; m/$what::[^:]/ || exit 1" -- "$appvars" || {
    echo "walkenfs: error: $what is a required parameter" 1>&2
    exit 1
  }
}

forbidden () {
  local what="$1"
  local vars="$2"

  perl -e "(\$_) = @ARGV; m/$what::[^:]/ && exit 1" -- "$appvars" || {
    echo "walkenfs: error: $what is a forbidden parameter" 1>&2
    exit 1
  }
}

appvars="$1"
mount_point="$2"
dash_o="$3"
mount_opts="$4"

HEART_COMMAND="'$0' '$1' '$2' '$3' '$4'"
export HEART_COMMAND

test "x$3" = x-o || exit 1

cookie=`optional -setcookie "$appvars" walken`
name=`optional -name "$appvars" walken`

if is_alive "$name"
  then
    echo "another walken server '$name' is already running." 1>&2
    exit 1
  fi

required -mnesia::dir "$appvars"

forbidden -walkenfs::mount_point "$appvars"
forbidden -walkenfs::mount_opts "$appvars"

args=`perl -e '($_) = @ARGV; 
               s/-setcookie::[^:]+//; 
               s/-name::[^:]+//; 
               s/::/ /g; 
               print' -- "$appvars"`

export PATH

ERL_CRASH_DUMP=/dev/null
export ERL_CRASH_DUMP

erl -heart -detached -setcookie "$cookie" -name $name $args -walkenfs mount_point "\"$2\"" -walkenfs mount_opts "\"$4\"" -s walkenfs 

# if you uncomment all this stuff, you can get a better grip for debugging
#
#touch /tmp/sux
#touch /tmp/sux.err
#exec 1>>/tmp/sux
#exec 2>>/tmp/sux.err
#
#(
#  erl -boot start_sasl -heart -noinput -noshell -setcookie "$cookie" -name $name $args -walkenfs mount_point "\"$2\"" -walkenfs mount_opts "\"$4\"" -s walkenfs &
#) &
